﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OplDataEntry.Models;

namespace OplDataEntry.Controllers
{
    public class MatchController : Controller
    {
        private ORITAEntities db = new ORITAEntities();

        //
        // GET: /Match/

        public ViewResult Index()
        {
            var playerteamseasons = db.PlayerTeamSeasons.Include("Player").Include("Player1").Include("Player2").Include("Player3").Include("Season").Include("Team").Include("Fixture");
            return View(playerteamseasons.ToList());
        }

        //
        // GET: /Match/Details/5

        public ViewResult Details(long id)
        {
            PlayerTeamSeason playerteamseason = db.PlayerTeamSeasons.Single(p => p.ID == id);
            return View(playerteamseason);
        }

        //
        // GET: /Match/Create

        public ActionResult Create()
        {
            //db.Crews.Where(p=>p.ID in  db.Players.Select(q=>q.CrewId));
            Dictionary<Crew, Player> pair = new Dictionary<Crew, Player>();
            pair = (from c in db.Crews
                    join p in db.Players
                    on c.ID equals p.CrewId
                    select new { p, c }).ToDictionary(q => q.c, q => q.p);

            ViewBag.BowledBy = new SelectList(pair.Keys, "ID", "Name");
            ViewBag.CaughtBy = new SelectList(pair.Keys, "ID", "Name");
            ViewBag.PlayerId = new SelectList(pair.Keys, "ID", "Name");
            ViewBag.RunOutBy = new SelectList(pair.Keys, "ID", "Name");
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName");
            ViewBag.TeamId = new SelectList(db.Teams, "ID", "TeamName");
            ViewBag.FixtureId = new SelectList(db.Fixtures, "ID", "ID");
            return View();
        }

        //
        // POST: /Match/Create

        [HttpPost]
        public ActionResult Create(PlayerTeamSeason playerteamseason)
        {
            if (ModelState.IsValid)
            {
                playerteamseason.BowledBy = playerteamseason.BowledBy.HasValue ? db.Players.Where(p => p.CrewId == playerteamseason.BowledBy.Value).First().ID : (long?)null;
                playerteamseason.CaughtBy = playerteamseason.CaughtBy.HasValue ? db.Players.Where(p => p.CrewId == playerteamseason.CaughtBy.Value).First().ID : (long?)null;
                playerteamseason.RunOutBy = playerteamseason.RunOutBy.HasValue ? db.Players.Where(p => p.CrewId == playerteamseason.RunOutBy.Value).First().ID : (long?)null;
                playerteamseason.PlayerId = db.Players.Where(p => p.CrewId == playerteamseason.PlayerId).First().ID;
                db.PlayerTeamSeasons.AddObject(playerteamseason);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.BowledBy = new SelectList(db.Players, "ID", "ID", playerteamseason.BowledBy);
            ViewBag.CaughtBy = new SelectList(db.Players, "ID", "ID", playerteamseason.CaughtBy);
            ViewBag.PlayerId = new SelectList(db.Players, "ID", "ID", playerteamseason.PlayerId);
            ViewBag.RunOutBy = new SelectList(db.Players, "ID", "ID", playerteamseason.RunOutBy);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", playerteamseason.SeasonId);
            ViewBag.TeamId = new SelectList(db.Teams, "ID", "TeamName", playerteamseason.TeamId);
            return View(playerteamseason);
        }

        //
        // GET: /Match/Edit/5

        public ActionResult Edit(long id)
        {
            PlayerTeamSeason playerteamseason = db.PlayerTeamSeasons.Single(p => p.ID == id);
            ViewBag.BowledBy = new SelectList(db.Players, "ID", "ID", playerteamseason.BowledBy);
            ViewBag.CaughtBy = new SelectList(db.Players, "ID", "ID", playerteamseason.CaughtBy);
            ViewBag.PlayerId = new SelectList(db.Players, "ID", "ID", playerteamseason.PlayerId);
            ViewBag.RunOutBy = new SelectList(db.Players, "ID", "ID", playerteamseason.RunOutBy);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", playerteamseason.SeasonId);
            ViewBag.TeamId = new SelectList(db.Teams, "ID", "TeamName", playerteamseason.TeamId);
            return View(playerteamseason);
        }

        //
        // POST: /Match/Edit/5

        [HttpPost]
        public ActionResult Edit(PlayerTeamSeason playerteamseason)
        {
            if (ModelState.IsValid)
            {
                db.PlayerTeamSeasons.Attach(playerteamseason);
                db.ObjectStateManager.ChangeObjectState(playerteamseason, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BowledBy = new SelectList(db.Players, "ID", "ID", playerteamseason.BowledBy);
            ViewBag.CaughtBy = new SelectList(db.Players, "ID", "ID", playerteamseason.CaughtBy);
            ViewBag.PlayerId = new SelectList(db.Players, "ID", "ID", playerteamseason.PlayerId);
            ViewBag.RunOutBy = new SelectList(db.Players, "ID", "ID", playerteamseason.RunOutBy);
            ViewBag.SeasonId = new SelectList(db.Seasons, "ID", "SeasonName", playerteamseason.SeasonId);
            ViewBag.TeamId = new SelectList(db.Teams, "ID", "TeamName", playerteamseason.TeamId);
            return View(playerteamseason);
        }

        //
        // GET: /Match/Delete/5

        public ActionResult Delete(long id)
        {
            PlayerTeamSeason playerteamseason = db.PlayerTeamSeasons.Single(p => p.ID == id);
            return View(playerteamseason);
        }

        //
        // POST: /Match/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            PlayerTeamSeason playerteamseason = db.PlayerTeamSeasons.Single(p => p.ID == id);
            db.PlayerTeamSeasons.DeleteObject(playerteamseason);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}